package org.leaf.green.b2b

class MarketPriceResearchController {

    def marketPriceResearchService

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        render view : '/b2b/market/price/research/list', model : [marketPriceResearchInstanceList: marketPriceResearchService.list(params), marketPriceResearchInstanceTotal: marketPriceResearchService.count()]
    }

    def create = {
        def marketPriceResearchInstance = marketPriceResearchService.init(params)
        //marketPriceResearchInstance.properties = params
        render view : '/b2b/market/price/research/create', model : [marketPriceResearchInstance: marketPriceResearchInstance]
    }

    def save = {
        def marketPriceResearchInstance = new MarketPriceResearch(params)
            marketPriceResearchInstance?.details?.each {detail ->
                detail.promotion = detail.promotion ?: false  
            }
        if (!marketPriceResearchInstance.hasErrors() && marketPriceResearchInstance.save()) {
            flash.message = "marketPriceResearch.created"
            flash.args = [marketPriceResearchInstance.id]
            flash.defaultMessage = "MarketPriceResearch ${marketPriceResearchInstance.id} created"
            redirect(action: "show", id: marketPriceResearchInstance.id)
        }
        else {
            render(view: "/b2b/market/price/research/create", model: [marketPriceResearchInstance: marketPriceResearchInstance])
        }
    }

    def show = {
        def marketPriceResearchInstance = MarketPriceResearch.get(params.id)
        if (!marketPriceResearchInstance) {
            flash.message = "marketPriceResearch.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceResearch not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/price/research/show', model : [marketPriceResearchInstance: marketPriceResearchInstance]
        }
    }

    def edit = {
        def marketPriceResearchInstance = MarketPriceResearch.get(params.id)
        if (!marketPriceResearchInstance) {
            flash.message = "marketPriceResearch.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceResearch not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            render view : '/b2b/market/price/research/edit', model : [marketPriceResearchInstance: marketPriceResearchInstance]
        }
    }

    def update = {
        def marketPriceResearchInstance = MarketPriceResearch.get(params.id)
        if (marketPriceResearchInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketPriceResearchInstance.version > version) {
                    
                    marketPriceResearchInstance.errors.rejectValue("version", "marketPriceResearch.optimistic.locking.failure", "Another user has updated this MarketPriceResearch while you were editing")
                    render(view: "/b2b/market/price/research/edit", model: [marketPriceResearchInstance: marketPriceResearchInstance])
                    return
                }
            }
            marketPriceResearchInstance.properties = params
            if (!marketPriceResearchInstance.hasErrors() && marketPriceResearchInstance.save()) {
                flash.message = "marketPriceResearch.updated"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceResearch ${params.id} updated"
                redirect(action: "show", id: marketPriceResearchInstance.id)
            }
            else {
                render(view: "/b2b/market/price/research/edit", model: [marketPriceResearchInstance: marketPriceResearchInstance])
            }
        }
        else {
            flash.message = "marketPriceResearch.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceResearch not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def marketPriceResearchInstance = MarketPriceResearch.get(params.id)
        if (marketPriceResearchInstance) {
            try {
                marketPriceResearchInstance.delete()
                flash.message = "marketPriceResearch.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceResearch ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "marketPriceResearch.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "MarketPriceResearch ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "marketPriceResearch.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketPriceResearch not found with id ${params.id}"
            redirect(action: "list")
        }
    }

    def approval = {
        def marketPriceResearchInstance = MarketPriceResearch.get(params.id)
        if (marketPriceResearchInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketPriceResearchInstance.version > version) {
                    marketPriceResearchInstance.errors.rejectValue("version", "marketResearch.optimistic.locking.failure", "Another user has updated this MarketResearch while you were editing")
                    redirect(action: "show", id: params.id)
                    return
                }
            }
            marketPriceResearchInstance.approved = true 
            if (!marketPriceResearchInstance.hasErrors() && marketPriceResearchInstance.save()) {
                flash.message = "marketResearch.approved"
                flash.args = [params.id]
                flash.defaultMessage = "MarketResearch ${params.id} updated"
                redirect(action: "show", id: marketPriceResearchInstance.id)
            }
            else {
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "marketResearch.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "MarketResearch not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }    
    }
    
}
